package com.umeox.watch.moto.dataservice.services;

import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.umeox.moto.common.log.LogUtils;
import com.umeox.moto.common.log.Logger;
import com.umeox.moto.mqtt.base.AError;
import com.umeox.moto.mqtt.base.ARequest;
import com.umeox.moto.mqtt.base.AResponse;
import com.umeox.watch.moto.dataservice.db.AppDataBase;
import com.umeox.watch.moto.dataservice.db.model.LocationInfo;
import com.umeox.watch.moto.dataservice.mqtt.interfaces.IPublishListener;

import java.util.concurrent.atomic.AtomicInteger;

public class ReuploadService extends IntentService {

    private static final String TAG = "Reupload-Service";

    private static final String ACTION_REUPLOAD_LOCATION = "REUPLOAD_LOCATION";

    private AtomicInteger failures;

    /**
     * 网络状态恢复，查找未上传成功的位置记录并重新上传
     */
    public static void reuploadLocations(Context context) {
        Intent intent = new Intent(context, ReuploadService.class);
        intent.setAction(ACTION_REUPLOAD_LOCATION);
        context.startService(intent);
    }

    public ReuploadService() {
        super(TAG);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        failures = new AtomicInteger();
        LogUtils.d(TAG, "开始重传任务");
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        String action = intent != null ? intent.getAction() : "";
        if (ACTION_REUPLOAD_LOCATION.equals(action)) {
//            List<LocationInfo> dtos = LocationInfoDao.getDao().queryUploadFailedRecords();
//            if (dtos != null && !dtos.isEmpty()) {
//                int failCounts = failures.addAndGet(dtos.size());
//                LogUtils.d(TAG, "上传失败的位置记录：" + failCounts);
//                for (LocationInfo locationInfo : dtos) {
//                    //上报位置
//                    PositionDTO dto = convert2PositionDTO(locationInfo);
//                    ApiService.uploadLocationInfo(locationInfo.getReportReason(), dto, new LocationUploadCallBack(locationInfo));
//                }
//            }
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.d(TAG, "重传任务完成");
    }


    private class LocationUploadCallBack implements IPublishListener {

        private LocationInfo mLocationInfo;

        public LocationUploadCallBack(LocationInfo locationInfo) {
            mLocationInfo = locationInfo;
        }

        @Override
        public void onSuccess(ARequest request, AResponse response) {
            Logger.i("上传位置成功");
            mLocationInfo.setReportFlag((byte) 1);
            AppDataBase.getInstance().locationInfoDao().update(mLocationInfo);

            LogUtils.d(TAG, "剩余未上传的位置记录：" + failures.decrementAndGet());
        }

        @Override
        public void onFailure(ARequest request, Object message, AError error) {
            Logger.i("上传位置失败,reason:" + error.getMsg());
        }
    }
}
